package com.settlement.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.settlement.system.mapper.SysBillColumnMapper;
import com.settlement.system.model.entity.SysBillColumn;
import com.settlement.system.model.query.BillColumnPageQuery;
import com.settlement.system.model.vo.BillConfigImportVO;
import com.settlement.system.service.SysBillColumnService;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 标准账单字段对应关系 服务实现类
 * </p>
 *
 * @author huangrensen
 * @since 2023-08-07
 */
@Service
public class SysBillColumnServiceImpl extends ServiceImpl<SysBillColumnMapper, SysBillColumn> implements SysBillColumnService {

    @Override
    public void importTableConfig(List<BillConfigImportVO> rows) {
        // 获取平台，把历史数据删除
        List<String> platformList = rows.stream().map(BillConfigImportVO::getPlatform).toList();
        this.remove(new LambdaQueryWrapper<SysBillColumn>()
                .in(SysBillColumn::getPlatform, platformList));
        // 批量保存数据
        for (BillConfigImportVO row : rows) {
            SysBillColumn billColumn = new SysBillColumn();
            BeanUtil.copyProperties(row, billColumn);
            this.save(billColumn);
        }
    }

    @Override
    public Page<SysBillColumn> getBillColumnPage(BillColumnPageQuery queryParams) {
        // 查询参数
        int pageNum = queryParams.getPageNum();
        int pageSize = queryParams.getPageSize();
        String keywords = queryParams.getKeywords();
        // 查询数据
        Page<SysBillColumn> billColumnPage = this.page(
                new Page<>(pageNum, pageSize),
                new LambdaQueryWrapper<SysBillColumn>()
                        .like(StrUtil.isNotBlank(keywords), SysBillColumn::getPlatform, keywords)
        );
        return billColumnPage;
    }

    @Override
    @Cacheable(value = "platform", key = "#platform")
    public SysBillColumn getByPlatform(String platform) {
        List<SysBillColumn> columnList = this.list(new LambdaQueryWrapper<SysBillColumn>()
                .like(StrUtil.isNotBlank(platform), SysBillColumn::getPlatform, platform)
        );
        if (columnList.size() > 0) {
            return columnList.get(0);
        }
        return null;
    }

    @Override
    public List<SysBillColumn> listAll() {
        return this.list();
    }
}
